# coding=utf-8
import qdarkstyle
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QIcon

from GUI.Widget.ToolBar import ToolBar
from GUI.MainWindow_design import Ui_MainWindow
from GUI.Widget.ImageWindow import ImageWindow
from GUI.Widget.ImageWindowList import ImageWindowList
import Tools
import GUI.Window.Editor.Widget as EditorWidget
import GUI.Menu as Menu


class MainWindow(QMainWindow, Ui_MainWindow):
    canvasShow = False
    canvasSize = (0, 0)
    trackMouseMove = False  # 不允许 自定义鼠标移动事件

    #      部件
    toolBar: ToolBar = None  # 工具条
    imageWindowList = None  # 图像工作区列表
    currentImageWindow: ImageWindow = None  # 当前图像工作窗体句柄

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.initUI()

    def initUI(self):
        # 主题
        self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
        # 得到桌面控件
        desktop = QApplication.desktop()
        # 得到屏幕可显示尺寸
        rect = desktop.availableGeometry()
        # 设置窗口尺寸
        self.resize(1600, 800)
        self.move((rect.width() - self.width()) / 2, (rect.height() - self.height()) / 2)
        self.setWindowTitle('Photo Sprite Little')
        self.setIconSize(QSize(64, 64))
        self.setWindowIcon(QIcon(':/logo/logo.png'))
        # self.showMaximized()
        self.show()

        # 创建菜单栏
        menuBar = Menu.MainWindowMenuBar(self)
        self.setMenuBar(menuBar)
        # 创建工具栏
        self.initToolBar()
        # 创建图像工作窗口列表
        self.imageWindowList = ImageWindowList(self)
        # 弹出窗口
        EditorWidget.init(self)

        self.statusBar().showMessage('准备就绪')

    def initToolBar(self):
        """
        :加载工具
        :窗体左边的工具
        :return:
        """
        # 工具栏
        toolBar = ToolBar(self)
        self.toolBar = toolBar
        # 编辑栏
        EditorWidget.init(self)

        # 移动工具
        tool = toolBar.toolButton_Move
        Tools.move.init(self, tool)
        # 文本工具
        tool = toolBar.toolButton_Text
        Tools.text.init(self, tool)

    def selectImageWindow(self, imageWindow):
        self.imageWindowList.setSelectedWindow(imageWindow.listItem)

    def newImageWindow(self, info={}, selected: bool=True):
        new = ImageWindow(self)
        self.imageWindowList.newWindow(new, info)
        if selected:
            self.selectImageWindow(new)
        return new

    def deleteImageWindow(self, imageWindow):
        if imageWindow:
            if self.currentImageWindow == imageWindow:
                flag = True
            else:
                flag = False
            self.imageWindowList.removeWindow(imageWindow.listItem)
            imageWindow.deleteLater()
            if flag:
                if self.imageWindowList.count() > 0:
                    self.imageWindowList.setSelectedWindow(self.imageWindowList.item(0))

    @property
    def currentLayer(self):
        if self.currentImageWindow:
            return self.currentImageWindow.currentLayer

    def resizeEvent(self,event):
        # 同步 图像工作窗体
        size = self.imagePanel.size()
        if self.currentImageWindow:
            # 让控制器面板跟随父对象变化
            self.currentImageWindow.setGeometry(0, 0, size.width(), size.height())
        # 图像工作区列表
        if self.imageWindowList:
            self.imageWindowList.updateSize()
